package com.totalinlink.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ClickableSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.totalinlink.blogic.LoginBLogic;

import com.totalinlink.sisclient.MainActivity;
import com.totalinlink.sisclient.R;
import com.totalinlink.sisclient.RegistActivity;

/**
 * Created by zhangpf1127 on 2015/9/6.
 */
public class LoginFragment extends Fragment implements OnClickListener {

    private final Context context;
    private EditText etUserId;
    private EditText etPwd;
    private Button btLogin;
    private ImageButton btTitle;
    private TextView tvTitle;
//    private TextView tvRegist;
    private RelativeLayout rlRegist;

    public LoginFragment(Context context) {
        this.context = context;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view=inflater.inflate(R.layout.login, null);

//        View menu=inflater.inflate(R.layout.menufragment_black, null);

        etUserId = (EditText)view.findViewById(R.id.username_edit);
        etPwd = (EditText)view.findViewById(R.id.password_edit);
        btLogin = (Button)view.findViewById(R.id.login_button);
        RelativeLayout titleLayout = (RelativeLayout)view.findViewById(R.id.include);
        btTitle = (ImageButton)titleLayout.findViewById(R.id.bt_title);
        tvTitle = (TextView)titleLayout.findViewById(R.id.tv_title);
//        tvRegist = (TextView)view.findViewById(R.id.tv_regist);
        rlRegist = (RelativeLayout)view.findViewById(R.id.rl_regist);

        btTitle.setOnClickListener(this);
        tvTitle.setText("登录君");

//        String registText = "注册";
//        SpannableString spannedString = new SpannableString(registText);
//        spannedString.setSpan(new ClickableSpan() {
//            @Override
//            public void onClick(View widget) {
//                Toast.makeText(context,"登陆成功！",Toast.LENGTH_SHORT).show();
//                Intent intent = new Intent(context, RegistActivity.class);
//                context.startActivity(intent);
//            }
//        }, 0, registText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//        tvRegist.setText(spannedString);
        rlRegist.setOnClickListener(this);

        btLogin.setOnClickListener(this);

        return view;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login_button:
                String resultString = new LoginBLogic().LoginBLogic(etUserId.getText().toString().trim(), etPwd.getText().toString().trim());
                if (resultString.equals("success")) {
                    MainActivity ra = (MainActivity) getActivity();
                    ra.initSlidingMenu();
                    ra.switchContent(new HomeFragment());
                    Toast.makeText(context,"登陆成功！",Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(context,"用户名或密码不对",Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.bt_title:
                MainActivity ra = (MainActivity) getActivity();
                ra.getSlidingMenu().toggle();
                break;
            case R.id.rl_regist:
                Intent intent = new Intent(context, RegistActivity.class);
                startActivity(intent);
                break;
        }
    }

}
